<template>
  <div class="date-teacher">
    <el-row :gutter="1" type="flex" justify="center" style="width:100%;">
      <el-col>
        <el-input v-model="keyword" placeholder="输入课程/学员名称" style="width:200px;" @keyup="handleFilter"/>
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button size="medium" type="info" @click="clearParams">清空</el-button>
      </el-col>
    </el-row>
    <eltable
      :table-label="tableLabel"
      :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="isEnd" label="取消预约课次" align="center" min-width="5">
        <template slot-scope="scope">
          <el-button type="text" @click="cancelDialog = true">{{ scope.row.cancleNum }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="button" label="操作" align="center" min-width="15">
        <template slot-scope="scope">
          <el-button type="primary" @click="detail(scope.row)">详情</el-button>
          <el-button type="primary" @click="handleCLickAdd(scope.row)">增加课次</el-button>
        </template>
      </el-table-column>
    </eltable>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <!-- 取消预约课次 -->
    <el-dialog :visible.sync="cancelDialog" title="取消预约记录" width="580px">
      <eltable :table-label="cancelLabel" :table-data="cancelData"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 增加课次 -->
    <el-dialog :visible.sync="dialog" title="增加课次" width="580px">
      <el-form :model="dialogData" label-width="160px">
        <el-form-item label="课程名称">
          <el-button type="text">{{ dialogData.courseName }}</el-button>
        </el-form-item>
        <el-form-item label="学员">{{ dialogData.userName }}</el-form-item>
        <el-form-item label="增加课次数">
          <el-input v-model="dialogData.number" style="width:200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Eltable from '@/components/Eltable'
import Pagination from '@/components/Pagination'
import { getStudentList, getStudentDetail, updateUserCourse } from '@/api/subject/course' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Eltable,
    Pagination
  },
  data() {
    return {
      dialog: false,
      cancelDialog: false,
      dialogData: {},
      total: 3,
      currentpage: 1,
      pagesize: 10,
      keyword: '',
      cancelLabel: [
        {
          label: '课程-课时名称', width: '15', prop: 'className'
        },
        {
          label: '老师名称', width: '15', prop: 'creatorName'
        },
        {
          label: '上课时间', width: '15', prop: 'classTime'
        }
      ],
      cancelData: [],
      tableLabel: [
        {
          label: '学员名称', width: '8', prop: 'userName'
        },
        {
          label: '在学课程', width: '15', prop: 'courseName'
        },
        {
          label: '可预约课次', width: '5', prop: 'reserveNum'
        },
        {
          label: '已预约课次', width: '5', prop: 'bespeakNum'
        },
        {
          label: '已完成课次', width: '5', prop: 'finishNum'
        }
      ],
      tableData: [],
      listFilter: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleClickSave() {
      // 保存课次
      this.dialogData.reserveNum = Number(this.dialogData.reserveNum) + Number(this.dialogData.number)
      updateUserCourse(this.dialogData).then(res => {
        this.$notify({
          title: '成功',
          message: '课次增加成功',
          type: 'success',
          duration: 2000
        })
        this.getData()
        this.dialog = false
      })
    },
    handleCLickAdd(row) {
      // 增加课次
      this.dialog = true
      this.dialogData = Object.assign({}, row)
    },
    handleSizeChange(val) {
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    getData() {
      getStudentList(getTenantId()).then(res => {
        this.tableData = res.data.items
        this.listFilter = res.data.items
        this.total = res.data.items.length
      })
    },
    handleFilter() {
      this.tableData = this.listFilter.filter(item => {
        if (this.keyword &&
          item.courseName.toLocaleLowerCase().indexOf(this.keyword.toLocaleLowerCase()) === -1 &&
          item.userName.toLocaleLowerCase().indexOf(this.keyword.toLocaleLowerCase()) === -1) return false
        return true
      })
    },
    clearParams() {
      this.keyword = ''
      this.getData()
    },
    detail(scope) {
      getStudentDetail(scope.row.userId).then(res => {
        console.log(res.data.items)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.date-teacher {
  margin-top: 0px;
  width: 100%;
  // padding: 14px 20px 21px 20px;
  .list {
    height: 110px;
    text-align: center;
  }
  .search {
    margin-top: 5px;
    padding: 15px 20px;
    background-color: #e5e7ea;
    border-radius: 5px;
  }
}
</style>
